<template>
	<view class="wf-box" :style="{ paddingTop: totalheight }">
		<HeadNav titleName="新的朋友" :topheight="navBarHeight"></HeadNav>

		<view class="main">
			<view class="main-ctr">
				<scroll-view scroll-y="true" class="scroll-Y" @scrolltolower="lower">
					<view v-for="(item,index) in applyList" class="group-item" @click="showChatBox(group)">
						<image :src="item.requestUserAvatar"></image>
						
						<view class="user-info">
							<view>{{userId==item.requestUserId? item.receiveUserName :item.requestUserName }}</view>
							<view class="user-id">ID: {{userId==item.requestUserId? item.receiveUserId: item.requestUserId }}</view>
						</view>
						<!-- {{ item.expirationTime }} -->
						<uni-icons class="icon" :type="userId==item.requestUserId?'upload':'download'" size="24" color="#999999"></uni-icons>
						<view class="item-operation">
							<template v-if="timeProcessing(item.expirationTime)">
								<view class="already-status">已过期</view>
							</template>
							<template v-else>
								<view class="occurrence-status" v-if="item.status == 0" @click.stop="agreeFriend(item.requestUserId)">
									{{userId==item.requestUserId? '等待通过':'添加'}}
									</view>
								<view class="already-status" v-else>已添加</view>
							</template>
							
						</view>
					</view>
				</scroll-view>



			</view>
		</view>
	</view>
</template>

<script>
	import { friendRequestListApi , friendRequestByRequestApi } from '@/api/api.js'
	import http from '@/common/request';
	import HeadNav from "@/components/HeadNav"
	import navBarMixin from '@/mixin/navBarMixin.js'
	export default {
		data() {
			return {
				page: {
					pageNum: 1,
					pageSize: 20
				},
				applyList: [],
				isAll: 0,
				userId: uni.getStorageSync('userInfo').id
			}
		},
		components: {
			HeadNav
		},
		mixins: [navBarMixin],
		methods: {
			showChatBox() {
				
			},
			async loadApplyList() {
				const res = await friendRequestListApi(this.page)
				if(!res.code) {
					this.total = res.data.total
					var info = this.applyList
					if (this.page.pageNum == 1) {
						this.applyList = res.data.records
					}else {
						this.applyList = info.concat(res.data.records)
					}
					this.isAll = this.applyList.length == this.total ? 0 : 1
					
				}
			},
			async agreeFriend(id) {
				if(id ==this.userId) return
				const res = await friendRequestByRequestApi(id)
				if(!res.code) {
					uni.showToast({
						title: '添加成功',
						icon: 'none'
					})
					this.loadApplyList()
				}else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				}
			},
			
			// 时间转时间戳
			timeProcessing(timeDate) {
				let nowTimeStamp = Date.parse(new Date()) // 当前时间戳
				// console.log('当前时间戳===',nowTimeStamp)
					
				let Time = new Date(timeDate);
				// console.log(Time)
				let timestemp = Time.getTime()
				
				if(nowTimeStamp >= timestemp) {
					return true
				}else {
					return false
				}
				// console.log('timestemp===',timestemp)
			},
			lower() {
				if(this.isAll) {
					this.page.pageNum++
					this.loadApplyList()
				}
			}
 		},
		onLoad() {
			this.loadApplyList()
			
			// this.timeProcessing()
			
		}
	}
</script>

<style lang="scss" scoped>
	.wf-box {
		overflow: hidden;

		.main {
			height: calc(100vh - 78px);
			padding: 0 30rpx;
			padding-bottom: 30rpx;
			padding-top: 30rpx;
			box-sizing: border-box;

			.main-ctr {
				height: 100%;
				padding: 0 30rpx;
				border-radius: 10rpx;
				background: #fff;

				.scroll-Y {
					height: 100%;
					overflow: auto;
					.icon {
						flex: 1;
						text-align: right;
						margin-right: 20rpx;
					}

					.group-item {
						display: flex;
						align-items: center;
						height: 120rpx;
						border-bottom: 2rpx solid #eee;

						image {
							width: 60rpx;
							height: 60rpx;
						}
						
						.user-info {
							margin-left: 20rpx;
							.user-id {
								font-size: 24rpx;
								color: #999;
							}
							
						}
						
						.item-operation {
							margin-left: auto;
							view {
								padding: 10rpx 18rpx;
								font-size: 24rpx;
								border-radius: 10rpx;
								text-align: center;
								&.occurrence-status {
									background: #4790e0;
									color: #fff;
								}
								
								&.already-status {
									background: #eee;
									color: #666;
								}
								
							}
						}

						text {
							// flex: 1;
							text-overflow: ellipsis;
							overflow: hidden;
							word-break: break-all;
							white-space: nowrap;
							margin-left: 20rpx;
							color: #333;
							font-size: 32rpx;
						}

						// image {
						// 	width: 60rpx;
						// 	height: 60rpx;
						// }
					}
				}


			}
		}
	}
</style>